﻿//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Common
//  File name:	DataEntityMapper.cs
//  Developer:	Solomatov Igor
//  Created:	15.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using Smart.Common.Entities.Mapper;

namespace Smart.Common.Entities
{
	public class DataEntityMapper : EntityMapper
	{
		public static DataEntityMapper Current = new DataEntityMapper();

		public override MapperClassBuilderConfig GetConfig()
		{
			var config = new MapperClassBuilderConfig
							{
								Usings = "using Microsoft.SharePoint;\r\nusing Smart.Common.Entities;\r\nusing System.Data;",
								SourceType = "DataRowView"
							};

			config.FillTempls[FieldMappingType.Identifier] = "e.{0} = Identifier.Empty;";
			config.FillTempls[FieldMappingType.String] = "e.{0} = null;";
			config.FillTempls[FieldMappingType.DateTime] = "e.{0} = DateTime.MinValue;";
			config.FillTempls[FieldMappingType.Int] = "e.{0} = 0;";
			config.FillTempls[FieldMappingType.Double] = "e.{0} = 0;";
			config.FillTempls[FieldMappingType.Bool] = "e.{0} = false;";
			config.FillTempls[FieldMappingType.Lookup] = "e.{0} = new LookupValue(Identifier.Empty);";
			config.FillTempls[FieldMappingType.LookupMulti] = "e.{0} = throw new NotSupportedException();";
			config.FillTempls[FieldMappingType.User] = "e.{0} = new UserValue(Identifier.Empty);";
			config.FillTempls[FieldMappingType.UserMulti] = "e.{0} = throw new NotSupportedException();";
			config.FillTempls[FieldMappingType.Link] = "e.{0} = new LinkValue(null, null);";
			config.FillTempls[FieldMappingType.ChoiceMulti] = "e.{0} = throw new NotSupportedException();";

			config.PreLoad = "\t\t\tstring tmp;";
			config.LoadTempls[FieldMappingType.Identifier] = "e.{0} = new Identifier(s[\"{1}\"]);";
			config.LoadTempls[FieldMappingType.String] = "e.{0} = (string)s[\"{1}\"];";
			config.LoadTempls[FieldMappingType.DateTime] = "e.{0} = s[\"{1}\"].SToDT(DateTime.MinValue);";
			config.LoadTempls[FieldMappingType.Int] = "e.{0} = s[\"{1}\"].SToInt(0);";
			config.LoadTempls[FieldMappingType.Double] = "e.{0} = s[\"{1}\"].SToDbl(0);";
			config.LoadTempls[FieldMappingType.Bool] = "e.{0} = s[\"{1}\"].SToBool(false);";
			config.LoadTempls[FieldMappingType.Lookup] =
				"tmp = SPH.GetItemValueStr(s, \"{1}\"); e.{0} = (tmp.IsNullOrEmpty() ? new LookupValue(Identifier.Empty) : new SPFieldLookupValue(tmp).CvtL());";
			config.LoadTempls[FieldMappingType.LookupMulti] =
				"tmp = SPH.GetItemValueStr(s, \"{1}\"); e.{0} = (tmp.IsNullOrEmpty() ? new LookupValueCollection() : new SPFieldLookupValueCollection(tmp).CvtL());";
			config.LoadTempls[FieldMappingType.User] = "tmp = SPH.GetItemValueStr(s, \"{1}\"); e.{0} = (tmp.IsNullOrEmpty() ? new UserValue(Identifier.Empty) : new SPFieldLookupValue(tmp).CvtU());";
			config.LoadTempls[FieldMappingType.UserMulti] =
				"tmp = SPH.GetItemValueStr(s, \"{1}\"); e.{0} = (tmp.IsNullOrEmpty() ? new UserValueCollection() : new SPFieldLookupValueCollection(tmp).CvtU());";
			config.LoadTempls[FieldMappingType.Link] = "tmp = SPH.GetItemValueStr(s, \"{1}\"); e.{0} = (tmp.IsNullOrEmpty() ? new LinkValue(null, null) : new SPFieldUrlValue(tmp).CvtL());";
			config.LoadTempls[FieldMappingType.ChoiceMulti] =
				"tmp = SPH.GetItemValueStr(s, \"{1}\"); e.{0} = (tmp.IsNullOrEmpty() ? new ChoiceValueCollection() : new SPFieldMultiChoiceValue(tmp).CvtC());";

			config.SaveTempls[FieldMappingType.Identifier] = "s.Values[\"{1}\"] = e.{0}.Int;";
			config.SaveTempls[FieldMappingType.String] = "s.Values[\"{1}\"] = e.{0};";
			config.SaveTempls[FieldMappingType.DateTime] = "s.Values[\"{1}\"] = (e.{0} == DateTime.MinValue ? null : (object)e.{0});";
			config.SaveTempls[FieldMappingType.Int] = "s.Values[\"{1}\"] = e.{0};";
			config.SaveTempls[FieldMappingType.Double] = "s.Values[\"{1}\"] = e.{0};";
			config.SaveTempls[FieldMappingType.Bool] = "s.Values[\"{1}\"] = e.{0} ? 1 : 0;";
			config.SaveTempls[FieldMappingType.Lookup] = "s[\"{1}\"] = e.{0}.ID.Int;";
			config.SaveTempls[FieldMappingType.LookupMulti] = "s[\"{1}\"] = e.{0}.ToString();";
			config.SaveTempls[FieldMappingType.User] = "s[\"{1}\"] = e.{0}.ID.Int;";
			config.SaveTempls[FieldMappingType.UserMulti] = "s[\"{1}\"] = e.{0}.ToString();";
			config.SaveTempls[FieldMappingType.Link] = "s[\"{1}\"] = e.{0}.ToString();";
			config.SaveTempls[FieldMappingType.ChoiceMulti] = "s[\"{1}\"] = e.{0}.ToString();";

			return config;
		}
	}
}